package homework014;

import java.util.concurrent.atomic.AtomicInteger;

public class Person implements Runnable {

    /**
     * 剩余竞拍次数
     */
    private AtomicInteger count = new AtomicInteger(50);

    /**
     * 当前报价
     */
    private AtomicInteger currentPrice = new AtomicInteger(0);

    /**
     * 当前报价线程
     */
    private String currentThread = "";

    @Override
    public void run() {
        while (true) {
            if (count.get() <= 0) {
                if (currentThread.equals(Thread.currentThread().getName())) {
                    System.out.println("中标者:" + currentThread + "，中标价为：" + currentPrice);
                }
                return;
            }

            // 执行加价
            int newPrice = currentPrice.getAndAdd(RandomUtils.getRandom(100, 4999));

            // 扣除一次竞拍次数
            count.decrementAndGet();

            // 修改当前报价线程
            currentThread = Thread.currentThread().getName();

            System.out.println(currentThread + "报价" + newPrice);
        }
    }
}
